package com.zfzs.post.service.impl;

import com.zfzs.post.model.dto.IdDTO;
import com.zfzs.post.model.dto.InsertAgentAreaDTO;
import com.zfzs.post.model.dto.UpdateAgentAreaDTO;
import com.zfzs.post.model.vo.AddressVo;
import com.zfzs.post.modules.address.domain.AgentArea;
import com.zfzs.post.modules.address.domain.SysCity;
import com.zfzs.post.modules.address.service.AgentAreaService;
import com.zfzs.post.modules.address.service.SysCityService;
import com.zfzs.post.modules.agent.service.dto.AgentDTO;
import com.zfzs.post.service.AddressService;
import com.zfzs.post.service.JwtUserDetailsService;
import com.zfzs.post.utils.CloneUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 地址接口实现类
 *
 * @author: ck
 * @create: 2019-09-09 17:03
 **/
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private SysCityService sysCityService;

    @Autowired
    private AgentAreaService agentAreaService;

    @Autowired
    private JwtUserDetailsService jwtUserDetailsService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertAddress(InsertAgentAreaDTO dto) {
        Long area = dto.getArea();
        Long city = dto.getCity();
        String info = dto.getInfo();
        boolean isDefault = dto.getIsDefault();
        String names = dto.getNames();
        String phone = dto.getPhone();
        Long province = dto.getProvince();
        AgentDTO agent = jwtUserDetailsService.getAgentInfo();
        if (isDefault) {
            //取消所有的默认
            agentAreaService.cancelDefaultByAgentId(agent.getId());
        }
        AgentArea agentArea = new AgentArea();
        agentArea.setAgentId(agent.getId());
        agentArea.setArea(area);
        agentArea.setCity(city);
        agentArea.setIsDefault(isDefault);
        agentArea.setProvince(province);
        agentArea.setPhone(phone);
        agentArea.setInfo(info);
        agentArea.setNames(names);
        agentAreaService.create(agentArea);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAddress(UpdateAgentAreaDTO dto) {
        Long id = dto.getId();
        Long area = dto.getArea();
        Long city = dto.getCity();
        String info = dto.getInfo();
        boolean isDefault = dto.getIsDefault();
        String names = dto.getNames();
        String phone = dto.getPhone();
        Long province = dto.getProvince();
        AgentDTO agent = jwtUserDetailsService.getAgentInfo();
        if (isDefault) {
            //取消所有的默认
            agentAreaService.cancelDefaultByAgentId(agent.getId());
        }
        AgentArea agentArea = new AgentArea();
        agentArea.setAgentId(agent.getId());
        agentArea.setAreaId(id);
        agentArea.setAgentId(agent.getId());
        agentArea.setArea(area);
        agentArea.setCity(city);
        agentArea.setIsDefault(isDefault);
        agentArea.setProvince(province);
        agentArea.setPhone(phone);
        agentArea.setInfo(info);
        agentArea.setNames(names);
        agentAreaService.update(agentArea);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAddress(IdDTO dto) {
        Long id = dto.getId();
        agentAreaService.deleteAreaById(id);
    }

    @Override
    public Map<String, Object> queryAreaList() {
        Map<String, Object> map = new HashMap<>();
        AgentDTO agent = jwtUserDetailsService.getAgentInfo();
        List<AgentArea> list = agentAreaService.queryAreaList(agent.getId());
        map.put("list", list);
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelDefault(IdDTO dto) {
        Long id = dto.getId();
        agentAreaService.cancelDefaultById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void settingDefault(IdDTO dto) {
        Long id = dto.getId();
        AgentDTO agent = jwtUserDetailsService.getAgentInfo();
        //取消所有的默认
        agentAreaService.cancelDefaultByAgentId(agent.getId());
        agentAreaService.defaultAreaById(id);
    }

    @Override
    public Map<String, Object> queryProvinceList() {
        Map<String, Object> map = new HashMap<>();
        List<SysCity> list = sysCityService.queryProvinceList();
        map.put("list", list);
        return map;
    }

    @Override
    public Map<String, Object> queryCityOrArea(IdDTO dto) {
        Map<String, Object> map = new HashMap<>();
        Long id = dto.getId();
        List<SysCity> list = sysCityService.queryCityOrArea(id);
        map.put("list", list);
        return map;
    }

    @Override
    public AddressVo getDefaultAddress() {
        AgentDTO info = jwtUserDetailsService.getAgentInfo();
        AgentArea area = agentAreaService.getDefault(info.getId());
        if (area == null) {
            return null;
        } else {
            return CloneUtils.clone(area, AddressVo.class);
        }
    }
}
